package com.zoe.onelink.core.util.http.model;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.http.HttpMethod;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-10-19
 */
@Data
@Accessors(chain = true)
public class HttpClientEntity {

    public HttpClientEntity() {
    }

    private HttpClientEntity(HttpMethod method, String url) {
        this.method = method;
        this.url = url;
    }

    private HttpMethod method;
    private String url;
    private Map<String, String> headerMap = new HashMap<>(3);
    private Map<String, String> formMap;
    private String body;
    private int connectTimeoutMillis;
    private int callTimeoutMillis;

    public HttpClientEntity addHeader(String key, String value) {
        this.headerMap.put(key, value);
        return this;
    }

    public HttpClientEntity addHeader(Map<String, String> headerMap) {
        if (headerMap != null) {
            this.headerMap.putAll(headerMap);
        }
        return this;
    }

    @Deprecated
    public HttpClientEntity setHeaderMap(Map<String, String> headerMap) {
        return addHeader(headerMap);
    }

    public static HttpClientEntity createGet(String url) {
        return new HttpClientEntity(HttpMethod.GET, url);
    }

    public static HttpClientEntity createGet(String url, Map<String, String> urlParamMap) {
        return new HttpClientEntity(HttpMethod.GET, url)
                .setFormMap(urlParamMap);
    }

    public static HttpClientEntity createPost(String url, Map<String, String> formMap) {
        return new HttpClientEntity(HttpMethod.POST, url)
                .setFormMap(formMap);
    }

    public static HttpClientEntity createPost(String url, String body) {
        return new HttpClientEntity(HttpMethod.POST, url)
                .setBody(body);
    }


}
